package com.hutou.chuangke.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.hutou.chuangke.MainApplication;
import com.hutou.chuangke.R;
import com.hutou.chuangke.net.HttpUtil;
import com.hutou.chuangke.net.RequestThreadPool;
import com.hutou.chuangke.net.http.User;
import com.hutou.chuangke.uitl.ActivityManager;
import com.hutou.chuangke.uitl.CommonUtils;
import com.hutou.chuangke.uitl.Utils;

/**
 * Created by tyhdark on 2017/3/21.
 * 注册
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    TextView right_tv,title_tv;
    ImageView back_image;
    Button rigisterBT;
    ImageView wechatImageview,qqImageview,weiboImageview;
    TextView forgetpwTv;
    CheckBox eyeCheckBox;
    EditText passwordET,accountET;
    String strpassword,straccount;
    Context mContext;
    RequestRegister requestRegister;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ActivityManager.getInstatnce().addActivity(this);//统一管理activity
        mContext = this;
        initView();
        requestRegister = new RequestRegister();
    }


    private void initView() {
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setVisibility(View.GONE);
        back_image = (ImageView) findViewById(R.id.back_image);
        back_image.setOnClickListener(this);
        title_tv = (TextView) findViewById(R.id.title_tv);
        title_tv.setText(getString(R.string.register));
        rigisterBT = (Button) findViewById(R.id.rigister_bt);
        wechatImageview = (ImageView) findViewById(R.id.wechat_imageview);
        wechatImageview.setOnClickListener(this);
        weiboImageview = (ImageView) findViewById(R.id.weibo_imageview);
        weiboImageview.setOnClickListener(this);
        qqImageview = (ImageView) findViewById(R.id.qq_imageview);
        qqImageview.setOnClickListener(this);
        forgetpwTv = (TextView) findViewById(R.id.forgetpw_textview);
        forgetpwTv.setOnClickListener(this);
        forgetpwTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        forgetpwTv.getPaint().setAntiAlias(true);//抗锯齿
        eyeCheckBox = (CheckBox) findViewById(R.id.eye_checkbox);
        eyeCheckBox.setOnCheckedChangeListener(this);
        accountET = (EditText) findViewById(R.id.account_et);
        passwordET = (EditText) findViewById(R.id.password_et);
        rigisterBT.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == wechatImageview){

        }else if(v == weiboImageview){

        }else if(v == qqImageview){

        }else if(v == forgetpwTv){

        }else if(v == rigisterBT){
//            Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
//            startActivity(intent);
//            finish();
            strpassword = passwordET.getText().toString();
            straccount = accountET.getText().toString();
            if(straccount.isEmpty()){
                Toast.makeText(mContext,"请填写手机号",Toast.LENGTH_SHORT).show();
                return;
            }else if(strpassword.isEmpty()){
                Toast.makeText(mContext,"请填写密码",Toast.LENGTH_SHORT).show();
                return;
            }
            requestRegister.execute();
        }else if(v == back_image){
            Intent intent = new Intent(RegisterActivity.this,LoginRegisterActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView == eyeCheckBox) {
            if (eyeCheckBox.isChecked()) {
                passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
    }

    class RequestRegister extends AsyncTask<Void , Void ,Boolean> {

        @Override
        protected Boolean doInBackground(Void... voids) {
            try{
                String Sid = Utils.md5Password(straccount+strpassword+"ZK");
                Log.d("tyhdark","sid="+Sid);
                JsonObject jsonObject = HttpUtil.getResposeJsonObject(User.register(straccount,strpassword,Sid,"123"));
                String flag = jsonObject.get("flag").getAsString();
                if(flag.equals("1")){
                    return true;
                }else{
                    return false;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if(aBoolean){
                Toast.makeText(mContext,"注册成功",Toast.LENGTH_SHORT).show();
                finish();
            }else{
                Toast.makeText(mContext,"注册失败",Toast.LENGTH_SHORT).show();
            }
        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(requestRegister != null){
            requestRegister.cancleTask();
        }
    }
}
